package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.bean.GoodsBean;

import java.util.List;

/**
 * 商品列表适配器（带Footer）
 * 用于新增订单页面
 */
public class GoodsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_GOODS = 0;
    private static final int TYPE_ADD_BUTTON = 1;

    private final Context mContext;
    private final GoodsAdapter mGoodsAdapter;
    private List<GoodsBean> mGoodsList;
    private OnFooterClickListener mFooterClickListener;

    public GoodsListAdapter(Context context) {
        mContext = context;
        mGoodsAdapter = new GoodsAdapter(context);
        mGoodsAdapter.setShowDelete(true);  // 显示删除按钮
    }

    public void setOnQuantityChangeListener(GoodsAdapter.OnQuantityChangeListener listener) {
        mGoodsAdapter.setOnQuantityChangeListener(listener);
    }

    public void setOnGoodsClickListener(GoodsAdapter.OnGoodsClickListener listener) {
        mGoodsAdapter.setOnGoodsClickListener(listener);
    }

    public void setOnFooterClickListener(OnFooterClickListener listener) {
        mFooterClickListener = listener;
    }

    public void setData(List<GoodsBean> data) {
        mGoodsList = data;
        mGoodsAdapter.setData(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int size = mGoodsList != null ? mGoodsList.size() : 0;
        return size + 1;  // 商品数量 + Footer
    }

    @Override
    public int getItemViewType(int position) {
        int goodsCount = mGoodsList != null ? mGoodsList.size() : 0;
        if (position < goodsCount) {
            return TYPE_GOODS;
        } else {
            return TYPE_ADD_BUTTON;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_GOODS) {
            return mGoodsAdapter.onCreateViewHolder(parent, viewType);
        } else {
            // Footer：新增商品按钮
            View view = LayoutInflater.from(mContext)
                .inflate(R.layout.new_order_footer_item, parent, false);
            return new FooterViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_GOODS) {
            GoodsAdapter.GoodsViewHolder goodsHolder = (GoodsAdapter.GoodsViewHolder) holder;
            goodsHolder.onBindView(position);
        }
    }

    private final class FooterViewHolder extends RecyclerView.ViewHolder {

        private FooterViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> {
                if (mFooterClickListener != null) {
                    mFooterClickListener.onFooterClick();
                }
            });
        }
    }

    /**
     * Footer 点击回调接口
     */
    public interface OnFooterClickListener {
        void onFooterClick();
    }
}

